package seg.tef4.whms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.scan.R;

import org.json.JSONObject;

import java.util.Objects;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import seg.tef4.whms.activity.activityinit.SubBaseActivity;
import seg.tef4.whms.apis.UserApi;
import seg.tef4.whms.domain.entity.SysUser;
import seg.tef4.whms.util.auth.OnlineUser;
import seg.tef4.whms.util.dialog.AlertDialogCreator;
import seg.tef4.whms.util.retrofitutil.RetrofitUtil;

public class IndexActivity extends SubBaseActivity {

    private ImageView avatar;
    private TextView nickname;
    private TextView userName;
    private TextView debtName;

    private Button spare_part_check_button;
    private Button out_warehouse_check_button;
    private Button equipment_check_button;

    private Button out_scan_check_button;
    private Button in_scan_check_button;
    private Button out_check_button;

    private UserApi userApi = (UserApi) RetrofitUtil.getApiInstance(UserApi.class);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        avatar = findViewById(R.id.index_avatar_imageview);
        nickname = findViewById(R.id.index_nickname_textview);
        userName = findViewById(R.id.index_userName_textview);
        debtName = findViewById(R.id.index_deptName_textview);
        getUserInfo();

        spare_part_check_button = findViewById(R.id.spare_part_check_button);
        out_warehouse_check_button = findViewById(R.id.out_warehouse_check_button);
        equipment_check_button = findViewById(R.id.equipment_check_button);
        out_scan_check_button = findViewById(R.id.out_scan_check_button);
        in_scan_check_button = findViewById(R.id.in_scan_check_button);
        out_check_button  = findViewById(R.id.out_check_button);
        buttonNavigateInit();

    }

    public static void startIndexActivity(Context context){
        context.startActivity(new Intent(context, IndexActivity.class));
    }

    private void getUserInfo(){
        String token = OnlineUser.getToken(IndexActivity.this);
        userApi.getInfo(token).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String resultJson = response.body().string();
                    JSONObject result = new JSONObject(resultJson);
                    int code = result.getInt("code");
                    String msg = result.getString("msg");
                    if(code == 200){
                        JSONObject jsonObject = result.getJSONObject("user");
                        SysUser sysUser = new SysUser(jsonObject.getLong("userId"),
                                jsonObject.getString("userName"),
                                jsonObject.getString("nickName"),
                                jsonObject.getString("avatar"),
                                jsonObject.getJSONObject("dept").getString("deptName"));
                        OnlineUser.setSysUser(sysUser);

                        nickname.setText("名称: " + sysUser.getNickName());
                        userName.setText("工号: " + sysUser.getUserName());
                        debtName.setText("部门: " + sysUser.getDeptName());
                        if(sysUser.getAvatar()!=null && !Objects.equals(sysUser.getAvatar(), ""))
                            Glide.with(IndexActivity.this)
                                    .load(RetrofitUtil.getBaseUrl() +
                                            sysUser.getAvatar().substring(1))
                                    .into(avatar);
                    }else{
                        String cmsg = new StringBuilder()
                                .append("错误码:\t").append(code).append("\n")
                                .append("消息:\t").append(msg)
                                .toString();
                        AlertDialogCreator.getConfirmDialog(IndexActivity.this,
                                "系统错误",cmsg,"确认",null).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    AlertDialogCreator.getConfirmDialog(IndexActivity.this,
                            "系统错误",e.toString(),"确认",null).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                System.out.println(t.toString());
                AlertDialogCreator.getConfirmDialog(IndexActivity.this,
                        "系统错误",t.toString(),"确认",null).show();
            }
        });
    }

    private void buttonNavigateInit(){
        spare_part_check_button.setOnClickListener(view -> {
            SparePartActivity.startSparePartActivity(IndexActivity.this);
        });
        out_warehouse_check_button.setOnClickListener(view -> {
            OutWarehouseActivity.startOutWarehouseActivity(IndexActivity.this);
        });
        equipment_check_button.setOnClickListener(view -> {
            EquipmentActivity.startEquipmentActivity(IndexActivity.this);
        });
        out_scan_check_button.setOnClickListener(view -> {
            OutScanActivity.startOutScanActivity(IndexActivity.this);
        });
        in_scan_check_button.setOnClickListener(view -> {
            InScanActivity.startInScanActivity(IndexActivity.this);
        });
        out_check_button.setOnClickListener(view -> {
            ReturnIndexActivity.startIndexActivity(IndexActivity.this);
        });

    }

}
